package com.xckj.cycling.viewmodel

import androidx.health.connect.client.HealthConnectClient
import androidx.health.connect.client.records.HeartRateRecord
import androidx.health.connect.client.records.SleepSessionRecord
import androidx.health.connect.client.records.SleepStageRecord
import androidx.health.connect.client.request.ReadRecordsRequest
import androidx.health.connect.client.time.TimeRangeFilter
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.google.gson.Gson
import com.junca.android.service.Request
import com.xckj.cycling.framework.model.Response
import com.xckj.cycling.model.HealthConfig
import com.xckj.cycling.model.HealthModel
import kotlinx.coroutines.launch
import java.time.Instant
import java.time.LocalDateTime

class HealthViewModel : ViewModel() {
    companion object {
        private const val TAG = "HealthViewModel"
    }
    // build a set of permissions for required data types


    fun loadData(client: HealthConnectClient, callback: (Response<Any>) -> Unit) {
        viewModelScope.launch {
            val time =
                Request.postSuspend<HealthConfig>("qixing/user/HealthConfig").data?.day ?: 1
            val filter = TimeRangeFilter.between(
                Instant.ofEpochMilli(System.currentTimeMillis() - (time * 1000 * 24 * 60 * 60L)),
                Instant.ofEpochMilli(System.currentTimeMillis())
            )
//            val step = client.readRecords(
//                ReadRecordsRequest<StepsRecord>(
//                    StepsRecord::class, filter
//                )
//            ).records.map {
//                HealthModel(
//                    it.count.toString(),
//                    it.startTime.toEpochMilli(),
//                    it.endTime.toEpochMilli()
//                )
//            }
//            val hrv = client.readRecords(
//                ReadRecordsRequest(
//                    HeartRateVariabilityRmssdRecord::class,
//                    filter
//                )
//            ).records.map {
//                HealthModel(
//                    it.heartRateVariabilityMillis.toString(),
//                    it.time.toEpochMilli(),
//                    it.time.toEpochMilli()
//                )
//            }
            val hr =
                client.readRecords(ReadRecordsRequest(HeartRateRecord::class, filter)).records.map {
                    HealthModel(
                        startTime = it.startTime.toEpochMilli() / 1000,
                        endTime = it.endTime.toEpochMilli() / 1000,
                        value = it.samples.firstOrNull()?.beatsPerMinute.toString(),
                    )
                }
            val sleep = client.readRecords(
                ReadRecordsRequest(
                    SleepSessionRecord::class,
                    filter
                )
            ).records.map {
                HealthModel(
                    endTime = it.endTime.toEpochMilli() / 1000,
                    startTime = it.startTime.toEpochMilli() / 1000,
                    type = 1
//                    type = when (it.stage) {
//                        SleepStageRecord.STAGE_TYPE_UNKNOWN -> {
//                            1
//                        }
//                        SleepStageRecord.STAGE_TYPE_AWAKE -> {
//                            2
//                        }
//                        SleepStageRecord.STAGE_TYPE_DEEP -> {
//                            4
//                        }
//                        SleepStageRecord.STAGE_TYPE_SLEEPING -> {
//                            3
//                        }
//                        SleepStageRecord.STAGE_TYPE_LIGHT -> {
//                            1
//                        }
//                        SleepStageRecord.STAGE_TYPE_OUT_OF_BED -> {
//                            0
//                        }
//                        SleepStageRecord.STAGE_TYPE_REM -> {
//                            5
//                        }
//                        else -> {
//                            1
//                        }
//                    }
                )
            }
            val r =
                Request.postSuspend<Any>("qixing/user/googleHealth", HashMap<String, Any>().apply {
                    put("json", HashMap<String, Any>().let {
//                        it.put("step", step)
//                        it.put("hrv", hrv)
                        it.put("sleep", sleep)
                        it.put("heart", hr)
                        Gson().toJson(it)
                    })
                })
            callback.invoke(r)
        }
    }

}